This rmd. file imports oxygen and voltage data from Google sheet Data is used to plot a oxygen and voltage gradient map of the winogradsky column Exponential and linear models were used to analyze data
#Loading Packages
library(tidyverse)
library(googledrive)
library(googlesheets4)
#library(ggplot2)
library(knitr)
library(minpack.lm)
library(broom)
library(purrr)
library(lme4)
library(patchwork)
#Importing from google sheet
googlesheets4::gs4_deauth()
CleanData_Oxygen <- googlesheets4::read_sheet("https://docs.google.com/spreadsheets/d/1hNakCyqPsTNRFpf1lGjoBQ-papyZ6xHKT6mcvh3LMb8/edit?pli=1&gid=842906991#gid=842906991", "CleanData_Oxygen") |>
mutate(Sample = as.character(Sample))
✔ Reading from Microcosms.
✔ Range ''CleanData_Oxygen''.
kable(CleanData_Oxygen)
| Sample | Depth_cm | DO_µg_L | temp_C |
|---|---|---|---|
| 1004 | 0.0 | 276.0 | 21.2 |
| 1004 | 6.2 | 276.0 | 21.2 |
| 1004 | 8.4 | 275.0 | 21.2 |
| 1004 | 13.1 | 6.3 | 21.2 |
| 1004 | 22.0 | 3.4 | 21.2 |
| 1003 | 0.0 | 101.0 | 20.0 |
| 1003 | 5.8 | 62.0 | 20.0 |
| 1003 | 8.4 | 52.0 | 20.0 |
| 1003 | 13.1 | 29.0 | 20.0 |
| 1003 | 21.3 | 21.0 | 20.0 |
| 1002 | 0.0 | 80.0 | 19.8 |
| 1002 | 6.6 | 45.0 | 19.8 |
| 1002 | 9.3 | 26.0 | 19.8 |
| 1002 | 15.1 | 21.0 | 19.8 |
| 1002 | 21.9 | 19.0 | 19.8 |
| 1001_C | 0.0 | 190.0 | 19.0 |
| 1001_C | 6.9 | 106.0 | 19.0 |
| 1001_C | 10.8 | 99.0 | 19.0 |
| 1001_C | 16.6 | 57.0 | 19.0 |
| 1001_C | 22.7 | 25.0 | 19.0 |
| 1001_W | 0.0 | 103.0 | 19.0 |
| 1001_W | 6.7 | 34.0 | 19.0 |
| 1001_W | 10.2 | 21.0 | 19.0 |
| 1001_W | 15.2 | 11.0 | 19.0 |
| 1001_W | 22.8 | 8.0 | 19.0 |
googlesheets4::gs4_deauth()
CleanData_Voltage <- googlesheets4::read_sheet("https://docs.google.com/spreadsheets/d/1hNakCyqPsTNRFpf1lGjoBQ-papyZ6xHKT6mcvh3LMb8/edit?pli=1&gid=842906991#gid=842906991", "CleanData_Voltage") |>
mutate(Sample = as.character(Sample))
✔ Reading from Microcosms.
✔ Range ''CleanData_Voltage''.
kable(CleanData_Voltage)
| Sample | Depth_cm | Voltage_Water_10s_mv | Voltage_Water_Max_mv | Voltage_Tap_10s_mv | Voltage_Tap_Max_mv |
|---|---|---|---|---|---|
| 1004 | 0.0 | NA | NA | 121.0 | 121.0 |
| 1004 | 6.2 | 160.2 | 190 | -168.0 | -168.0 |
| 1004 | 8.4 | 74.4 | 79 | 84.0 | 122.0 |
| 1004 | 13.1 | -55.0 | 58 | 43.0 | 51.0 |
| 1004 | 22.0 | -200.0 | 204 | 84.0 | 102.0 |
| 1003 | 0.0 | NA | NA | 74.0 | 300.0 |
| 1003 | 5.8 | 27.0 | 27 | -13.0 | 26.0 |
| 1003 | 8.4 | 59.0 | 83 | 57.0 | 57.0 |
| 1003 | 13.1 | -20.0 | 101 | -94.0 | -136.0 |
| 1003 | 21.3 | -25.0 | -30 | 161.0 | 177.0 |
| 1002 | 0.0 | NA | NA | 51.0 | 199.0 |
| 1002 | 6.6 | -39.0 | -40 | 13.0 | 30.0 |
| 1002 | 9.3 | -4.3 | 30 | 196.0 | 199.0 |
| 1002 | 15.1 | 24.0 | -4 | 16.0 | 25.0 |
| 1002 | 21.9 | 27.0 | 28 | 194.0 | 199.0 |
| 1001_C | 0.0 | NA | NA | 187.0 | 187.0 |
| 1001_C | 6.9 | -10.0 | -22 | 76.0 | 300.0 |
| 1001_C | 10.8 | 25.0 | 60 | -2.8 | -2.8 |
| 1001_C | 16.6 | -6.0 | -23 | -10.5 | -3.0 |
| 1001_C | 22.7 | 6.0 | 48 | 168.0 | 174.0 |
| 1001_W | 0.0 | NA | NA | 187.0 | 187.0 |
| 1001_W | 6.7 | 16.0 | -9 | 13.0 | 170.0 |
| 1001_W | 10.2 | 31.0 | 33 | -48.0 | -48.0 |
| 1001_W | 15.2 | 34.0 | 40 | 24.0 | 24.0 |
| 1001_W | 22.8 | 40.0 | -17 | 72.0 | 72.0 |
Combine O2 & Voltage data by sample & depth
Gradients <- left_join(x = CleanData_Oxygen, y = CleanData_Voltage, by = c("Sample", "Depth_cm"))
#Oxygen Gradient Plot
ggplot(data = CleanData_Oxygen) +
geom_point(aes(y = DO_µg_L, x = Depth_cm)) +
#scale_y_reverse() +
#labs( title= "Oxygen Gradients", caption= "Figure X. Dissolved Oxygen (DO) (µg/L) measured in four Winogradsky colums at 5 depths (cm)")
#+ theme(plot.caption= element_text(size = 11, hjust=0)) +
#geom_path(aes(y = Depth_cm, x = DO_µg_L)) +
facet_grid(cols = vars(Sample)) +
theme_bw()
#+ scale_y_continuous(limits = c(0, 300))
#define exponential decay function for data fitting.
exp_decay <- function(x, i, mu){y = i * exp(mu * x)}
O2_nest <- CleanData_Oxygen |> #alternate forward pipe is %>% loaded with tidyverse
nest(.by = "Sample") |>
mutate(DecayFit = purrr::map(data, ~nlsLM(DO_µg_L ~ exp_decay(x = Depth_cm, i, mu),
data = .x)),
DecayTidy = purrr::map(DecayFit, tidy),
DecayParam = purrr::map(DecayFit, glance),
DecayPredict = purrr::map(DecayFit, augment)
)
Warning: There were 5 warnings in `mutate()`.
The first warning was:
ℹ In argument: `DecayFit = purrr::map(...)`.
Caused by warning in `nlsLM()`:
! No starting values specified for some parameters.
Initializing ‘i’, ‘mu’ to '1.'.
Consider specifying 'start' or using a selfStart model
ℹ Run ]8;;ide:run:dplyr::last_dplyr_warnings()dplyr::last_dplyr_warnings()]8;; to see the 4 remaining warnings.
O2_nest |>
unnest(cols = c(DecayPredict)) |>
ggplot() +
geom_point(aes(x = Depth_cm, y = DO_µg_L)) +
geom_line(aes(x = Depth_cm, y = .fitted)) +
geom_point(aes(x = Depth_cm, y = .resid), colour = "red") +
facet_grid(cols = vars(unlist(Sample))) +
theme_bw()
O2_nest |>
unnest(cols = c(DecayTidy)) |>
select(-c(data, DecayFit, DecayParam, DecayPredict)) |>
select(-c(statistic)) |>
pivot_wider(id_cols = Sample, names_from = term, values_from = c(estimate, std.error, p.value)) |>
kable()
| Sample | estimate_i | estimate_mu | std.error_i | std.error_mu | p.value_i | p.value_mu |
|---|---|---|---|---|---|---|
| 1004 | 326.62312 | -0.0779518 | 101.553082 | 0.0494649 | 0.0487222 | 0.2131346 |
| 1003 | 100.85694 | -0.0833277 | 3.557277 | 0.0059295 | 0.0000963 | 0.0007804 |
| 1002 | 78.61257 | -0.0897172 | 6.441339 | 0.0139884 | 0.0011845 | 0.0076805 |
| 1001_C | 190.11905 | -0.0743515 | 10.391455 | 0.0078457 | 0.0003563 | 0.0024909 |
| 1001_W | 102.49196 | -0.1553412 | 3.251191 | 0.0097456 | 0.0000701 | 0.0005369 |
ggplot(data = Gradients) +
geom_point(aes(x = Depth_cm, y = Voltage_Water_10s_mv)) +
facet_grid(cols = vars(Sample)) +
theme_bw()
Linear_fit <- Gradients %>%
group_by(Sample) %>%
group_modify(~ {
linear <- lm(Voltage_Water_10s_mv ~ Depth_cm, data = .x, na.action = na.exclude)
augment(linear, data = .x)}) %>%
ungroup()
Linear_fit |>
ggplot(aes(x = Depth_cm, y = Voltage_Water_10s_mv)) +
geom_point() +
geom_line(aes(y = .fitted)) +
#Showing residuals
#geom_point(aes(x = Depth_cm, y = .resid), colour = "red") +
geom_segment(aes(xend = Depth_cm, yend = .fitted), color = "red") +
facet_grid(cols = vars(unlist(Sample))) +
theme_bw()
NA
NA
Linear_fit_results <- Linear_fit |>
group_by(Sample) |>
group_modify(~ {
linear_model <- lm(Voltage_Water_10s_mv ~ Depth_cm, data = .x)
tidy_model <- tidy(linear_model)
tidy_model$Sample <- unique(.x$Sample)
return(tidy_model)
}) |>
ungroup() |>
# Filter for 'Depth_cm' coefficient (we are examining Voltage in terms of Depth_cm)
filter(term == "Depth_cm")
Warning: Unknown or uninitialised column: `Sample`.Warning: Unknown or uninitialised column: `Sample`.Warning: Unknown or uninitialised column: `Sample`.Warning: Unknown or uninitialised column: `Sample`.Warning: Unknown or uninitialised column: `Sample`.
# Selecting to include Sample, estimate of slope, std.error, and p.value in table output
Linear_fit_results |>
select(Sample, estimate, std.error, p.value)
#Displaying the table and specifying sig digits shown
Linear_fit |>
kable(digits=3)
| Sample | Depth_cm | DO_µg_L | temp_C | Voltage_Water_10s_mv | Voltage_Water_Max_mv | Voltage_Tap_10s_mv | Voltage_Tap_Max_mv | .fitted | .resid | .hat | .sigma | .cooksd | .std.resid |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1001_C | 0.0 | 190.0 | 19.0 | NA | NA | 187.0 | 187.0 | NA | NA | 0.000 | 19.193 | NA | NA |
| 1001_C | 6.9 | 106.0 | 19.0 | -10.0 | -22 | 76.0 | 300.0 | 2.523 | -12.523 | 0.628 | 17.748 | 0.967 | -1.070 |
| 1001_C | 10.8 | 99.0 | 19.0 | 25.0 | 60 | -2.8 | -2.8 | 3.174 | 21.826 | 0.333 | 4.714 | 0.485 | 1.393 |
| 1001_C | 16.6 | 57.0 | 19.0 | -6.0 | -23 | -10.5 | -3.0 | 4.142 | -10.142 | 0.289 | 24.334 | 0.080 | -0.627 |
| 1001_C | 22.7 | 25.0 | 19.0 | 6.0 | 48 | 168.0 | 174.0 | 5.161 | 0.839 | 0.750 | 27.092 | 0.011 | 0.087 |
| 1001_W | 0.0 | 103.0 | 19.0 | NA | NA | 187.0 | 187.0 | NA | NA | 0.000 | 5.575 | NA | NA |
| 1001_W | 6.7 | 34.0 | 19.0 | 16.0 | -9 | 13.0 | 170.0 | 21.056 | -5.056 | 0.587 | 0.463 | 1.418 | -1.412 |
| 1001_W | 10.2 | 21.0 | 19.0 | 31.0 | 33 | -48.0 | -48.0 | 25.637 | 5.363 | 0.335 | 4.349 | 0.350 | 1.180 |
| 1001_W | 15.2 | 11.0 | 19.0 | 34.0 | 40 | 24.0 | 24.0 | 32.180 | 1.820 | 0.265 | 7.593 | 0.026 | 0.381 |
| 1001_W | 22.8 | 8.0 | 19.0 | 40.0 | -17 | 72.0 | 72.0 | 42.127 | -2.127 | 0.813 | 6.164 | 1.689 | -0.882 |
| 1002 | 0.0 | 80.0 | 19.8 | NA | NA | 51.0 | 199.0 | NA | NA | 0.000 | 17.362 | NA | NA |
| 1002 | 6.6 | 45.0 | 19.8 | -39.0 | -40 | 13.0 | 30.0 | -24.681 | -14.319 | 0.568 | 11.331 | 1.034 | -1.255 |
| 1002 | 9.3 | 26.0 | 19.8 | -4.3 | 30 | 196.0 | 199.0 | -13.838 | 9.538 | 0.362 | 21.457 | 0.134 | 0.688 |
| 1002 | 15.1 | 21.0 | 19.8 | 24.0 | -4 | 16.0 | 25.0 | 9.455 | 14.545 | 0.275 | 17.632 | 0.184 | 0.984 |
| 1002 | 21.9 | 19.0 | 19.8 | 27.0 | 28 | 194.0 | 199.0 | 36.764 | -9.764 | 0.795 | 11.736 | 2.993 | -1.242 |
| 1003 | 0.0 | 101.0 | 20.0 | NA | NA | 74.0 | 300.0 | NA | NA | 0.000 | 30.529 | NA | NA |
| 1003 | 5.8 | 62.0 | 20.0 | 27.0 | 27 | -13.0 | 26.0 | 39.506 | -12.506 | 0.540 | 39.039 | 0.214 | -0.604 |
| 1003 | 8.4 | 52.0 | 20.0 | 59.0 | 83 | 57.0 | 57.0 | 27.527 | 31.473 | 0.351 | 18.369 | 0.443 | 1.280 |
| 1003 | 13.1 | 29.0 | 20.0 | -20.0 | 101 | -94.0 | -136.0 | 5.873 | -25.873 | 0.256 | 31.044 | 0.167 | -0.983 |
| 1003 | 21.3 | 21.0 | 20.0 | -25.0 | -30 | 161.0 | 177.0 | -31.906 | 6.906 | 0.852 | 39.258 | 0.999 | 0.589 |
| 1004 | 0.0 | 276.0 | 21.2 | NA | NA | 121.0 | 121.0 | NA | NA | 0.000 | 34.359 | NA | NA |
| 1004 | 6.2 | 276.0 | 21.2 | 160.2 | 190 | -168.0 | -168.0 | 132.396 | 27.804 | 0.513 | 27.789 | 0.710 | 1.160 |
| 1004 | 8.4 | 275.0 | 21.2 | 74.4 | 79 | 84.0 | 122.0 | 83.803 | -9.403 | 0.360 | 47.147 | 0.033 | -0.342 |
| 1004 | 13.1 | 6.3 | 21.2 | -55.0 | 58 | 43.0 | 51.0 | -20.009 | -34.991 | 0.253 | 26.867 | 0.235 | -1.178 |
| 1004 | 22.0 | 3.4 | 21.2 | -200.0 | 204 | 84.0 | 102.0 | -216.590 | 16.590 | 0.873 | 13.735 | 6.342 | 1.357 |
NA
Oxygen <- ggplot(data = Gradients) +
geom_point(aes(x = Depth_cm, y = DO_µg_L)) +
facet_grid(cols = vars(Sample)) +
theme_bw()
Voltage <- ggplot(data = Gradients) +
geom_point(aes(x = Depth_cm, y = Voltage_Water_10s_mv)) +
facet_grid(cols = vars(Sample)) +
theme_bw()
print(Oxygen/Voltage)
NA
NA